﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Brain
{
    public class GameScreen : IDrawable, IUpdateable, IBrainSonSerialiable
    {
        private List<IUpdateable> UpdateComponents;
        private List<IDrawable> DrawComponents;
        public List<IBaseComponent> AllComponents;

        public bool ShouldUpdate { get; set; }
        public bool ShouldDraw { get; set; }

        public bool ShouldSerialize { get; set; }

        public GameScreen()
        {
            UpdateComponents = new List<IUpdateable>();
            DrawComponents = new List<IDrawable>();
            AllComponents = new List<IBaseComponent>();

            ShouldDraw = true;
            ShouldUpdate = true;

            ShouldSerialize = true;

            Engine.Instance.ScreenManager.AddScreen(this);
        }

        public virtual void AddComponent(IBaseComponent component)
        {
            if (component is IUpdateable)
            {
                this.UpdateComponents.Add(component as IUpdateable);
            }
            if (component is IDrawable)
            {
                this.DrawComponents.Add(component as IDrawable);
            }
            AllComponents.Add(component);
        }
        
        public virtual void Update()
        {
            for (int i = 0; i < UpdateComponents.Count; i++)
            {
                IUpdateable component = UpdateComponents[i];
                if (component.ShouldUpdate)
                {
                    component.Update();
                }
            }
        }

        public virtual void Draw()
        {
            for (int i = 0; i < DrawComponents.Count; i++)
            {
                IDrawable component = DrawComponents[i];
                if (component.ShouldDraw)
                {
                    component.Draw();
                }
            }
        }

        public virtual void SerializeData(SerializationData parentData, int index)
        {
            parentData.AddData("GS.Components", AllComponents.Count, index);
            for (int i = 0; i < AllComponents.Count; i++)
            {
                IBaseComponent comp = AllComponents[i];
                if (comp.ShouldSerialize)
                {
                    Type t = comp.GetType();
                    parentData.AddData("GS.Component" + i, t.ToString(), index);
                    comp.SerializeData(parentData, i);
                }
            }
        }

        public virtual void DeserializeData(SerializationData parentData, int index)
        {
            int components = parentData.GetData<int>("GS.Components", index);
            for (int i = 0; i < components; i++)
            {
                string t = parentData.GetData<string>("GS.Component" + i, index);
                Type type = Type.GetType(t);
                IBaseComponent comp = (IBaseComponent)Activator.CreateInstance(type);
                this.AddComponent(comp);

                comp.DeserializeData(parentData, i);
            }
        }
    }
}
